package com.ss.android.article.base.feature.detail2.a.b;

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.app.BizApplication;
import com.ss.android.article.base.feature.detail2.a.c.z;
import com.ss.android.article.base.feature.model.DownloadAppAd;
import com.ss.android.article.base.feature.model.g;
import com.ss.android.b.a.BaseAdsData;
import com.ss.android.common.download.DownloadAdapterProxy;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.download.DownloadInfoChangeListener;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.messagebus.Subscriber;
import com.ss.android.messagebus.SubscriptionRouter;
import com.ss.android.newmedia.a.s;
import com.ss.android.newmedia.util.AppUtil;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

public final class a {
    public static final Map a;

    static {
        a = new HashMap();
    }

    protected Context b;
    DownloadAppAd c;
    DownloadShortInfo d;
    a$a e;
    z f;
    private b g;
    private a$c h;
    private g i;
    public a(Context arg5, DownloadAppAd arg6) {

        this.h = null;
        this.e = null;
        this.i = null;
        this.b = arg5;
        AppData.getInstance();
        this.b.getResources();
        this.c = arg6;
        Object v0 = a.get(Long.valueOf(arg6.adId));
        this.i = ((g) v0);
        if (v0 == null) {
            this.e = new a$a(((BaseAdsData) arg6));
            this.i = new g(this.e);
        }
    }

    public final void a() {
        SubscriptionRouter.removeSubscriber(this);
        this.f = null;
    }

    public final void a(z arg1) {
        SubscriptionRouter.addSubscriber(this);
        this.f = arg1;
        this.d();
    }

    public final void b() {
        DownloadShortInfo v3 = null;
        int v5 = 5;
        if (this.c != null && this.f != null) {
            if (a.get(Long.valueOf(this.c.adId)) == null) {
                a.put(Long.valueOf(this.c.adId), this.i);
            }

            if (AppData.getInstance().allowInsideDownloadManager()) {
                this.c.a(this.b, 1, this.d, this.i, v5);
                return;
            }

            this.c.a(this.b, 1, v3, ((DownloadInfoChangeListener) null), v5);
        }
    }

    public final void c() {
        DownloadShortInfo v3 = null;
        int v5 = 5;
        int v2 = 2;
        if (this.c != null && this.f != null) {
            if (a.get(Long.valueOf(this.c.adId)) == null) {
                a.put(Long.valueOf(this.c.adId), this.i);
            }

            if (AppData.getInstance().allowInsideDownloadManager()) {
                this.c.a(this.b, v2, this.d, this.i, v5);
                return;
            }

            this.c.a(this.b, v2, v3, ((DownloadInfoChangeListener) null), v5);
        }
    }

    public final void d() {
        if (this.c != null && this.f != null) {
            this.h = new a$c(this);
            g v0 = this.i;
            a$c v1 = this.h;
            v0.a = new WeakReference(v1);
            ((DownloadInfoChangeListener) v1).setDownloadId(v0.b);
            if (this.g != null && this.g.getStatus() != AsyncTask.Status.FINISHED) {
                this.g.cancel(true);
            }

            this.g = new b(this);
            com.bytedance.common.utility.b.a.a(this.g, new String[]{this.c.downloadUrl});
        }
    }

    @Subscriber
    public final void getDownloadId(s arg8) {
        DownloadAdapterProxy.inst().registerDownloadListener(this.b, Long.valueOf(arg8.a), this.i, String.valueOf(this.c.adId), 5, this.c.logExtra);
    }

    final class a$a implements DownloadInfoChangeListener {
        com.ss.android.article.base.feature.detail2.a.aa a;
        private long b;
        private BaseAdsData c;

        public a$a(BaseAdsData arg3) {
            this.c = arg3;
            this.a = new com.ss.android.article.base.feature.detail2.a.aa(this.c);
        }

        public final void setDownloadId(long id) {
            this.b = id;
        }

        public final void onDownloadProgressChanged(DownloadShortInfo arg5, int arg6, long arg7, long arg9, long arg11) {
            if (arg5 != null && arg5.id == this.b) {
                int v0 = 0;
                if (arg6 == 3) {
                    if (arg5.b == 16) {
                        v0 = 16;
                    } else if (arg5.b == 8) {
                        v0 = ToolUtils.isAppInstalled(BizApplication.getApplication(), this.c.pkg) ? 32 : 8;
                    }

                    com.ss.android.article.base.feature.detail2.a.aa v1 = this.a;
                    String v2 = "detail_download_ad";
                    if (v0 == 8 && !v1.b) {
                        v1.a(v2, "download_finish");
                        v1.b = true;
                    } else if (v0 == 16) {
                        v1.a(v2, "download_failed");
                    }

                    com.ss.android.article.base.feature.detail2.a.b.a.a.remove(Long.valueOf(v1.c.id));
                }
            }
        }
    }

    final class b extends AsyncTask {
        private final a a;

        b(a arg1) {
            this.a = arg1;

        }

        protected final Object doInBackground(Object[] arg4) {
            DownloadShortInfo v0;
            if (arg4 != null) {
                if (arg4.length > 0 && (TextUtils.isEmpty((CharSequence) arg4[0]))) {
                    return null;
                } else if (this.a.b == null) {
                    return null;
                } else {
                    return DownloadAdapterProxy.inst().queryDownloadInfo(this.a.b, (String) arg4[0]);
                }
            } else {
               return null;
            }
        }

        protected final void onPostExecute(Object arg7) {
            super.onPostExecute(arg7);
            if (!this.isCancelled() && this.a.f != null) {
                boolean v0 = AppUtil.a(this.a.b, this.a.c.version, this.a.c.pkg);
                if (arg7 != null) {
                    try {
                        if (((DownloadShortInfo) arg7).id > -1 && !v0 && (!DownloadAdapterProxy.inst()
                                .isDownloadSuccessAndFileNotExist(this.a.b, ((DownloadShortInfo) arg7)) || (ToolUtils.isAppInstalled(BizApplication.getApplication(), this.a.c.pkg)))) {
                            this.a.d = this.a.d == null || this.a.d.b != 16 ? ((DownloadShortInfo) arg7) : null;
                            this.a.f.a(((DownloadShortInfo) arg7));
                            return;
                        }
                    } catch (Exception v0_1) {
                        v0_1.printStackTrace();
                    }

                    return;
                }

                if (ToolUtils.isAppInstalled(this.a.b, this.a.c.pkg)) {
                    this.a.f.a(this.a.d);
                    return;
                }

                this.a.d = null;
                if (this.a.e != null) {
                    com.ss.android.article.base.feature.detail2.a.aa v0_2 = this.a.e.a;
                    v0_2.b = false;
                    v0_2.a = false;
                }

                this.a.f.a(null);
                return;
            }
        }
    }

    final class a$c implements DownloadInfoChangeListener {
        private final a b;
        private long a;

        a$c(a arg1) {
            this.b = arg1;

        }

        public final void setDownloadId(long id) {
            this.a = id;
        }

        public final void onDownloadProgressChanged(DownloadShortInfo arg10, int arg11, long arg12, long arg14, long arg16) {
            if (arg10 == null || arg10.id != this.a || this.b.f == null) {
                Logger.d("DetailAdPresenter", "downloadInfoChange RETURN!");
            } else {
                this.b.f.a(arg10, arg11, arg12, arg14);
                this.b.d = arg10.b == 16 ? null : arg10;
            }
        }
    }
}

